home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / emula / arosdv19.lha / AROS / dos / fputc.c < prev    next >
C/C++ Source or Header  |  1996-10-24  |  3KB  |  140 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: fputc.c,v 1.5 1996/10/24 15:50:29 aros Exp $
  4.     $Log: fputc.c,v $
  5.     Revision 1.5  1996/10/24 15:50:29  aros
  6.     Use the official AROS macros over the __AROS versions.
  7.  
  8.     Revision 1.4  1996/08/13 13:52:46  digulla
  9.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  10.     Replaced AROS_LA by AROS_LHA
  11.  
  12.     Revision 1.3  1996/08/12 14:22:57  digulla
  13.     Removed irritating empty line
  14.  
  15.     Revision 1.2  1996/08/01 17:40:51  digulla
  16.     Added standard header for all files
  17.  
  18.     Desc:
  19.     Lang: english
  20. */
  21. #include <exec/memory.h>
  22. #include <clib/exec_protos.h>
  23. #include <dos/dosextens.h>
  24. #include "dos_intern.h"
  25.  
  26. /*****************************************************************************
  27.  
  28.     NAME */
  29.     #include <clib/dos_protos.h>
  30.  
  31.     AROS_LH2(LONG, FPutC,
  32.  
  33. /*  SYNOPSIS */
  34.     AROS_LHA(BPTR, file,      D1),
  35.     AROS_LHA(LONG, character, D2),
  36.  
  37. /*  LOCATION */
  38.     struct DosLibrary *, DOSBase, 52, Dos)
  39.  
  40. /*  FUNCTION
  41.  
  42.     INPUTS
  43.     file      - Filehandle to write to.
  44.     character - Character to write.
  45.  
  46.     RESULT
  47.     The character written or EOF in case of an error.
  48.     IoErr() gives additional information in that case.
  49.  
  50.     NOTES
  51.  
  52.     EXAMPLE
  53.  
  54.     BUGS
  55.  
  56.     SEE ALSO
  57.     FGetC(), IoErr()
  58.  
  59.     INTERNALS
  60.  
  61.     HISTORY
  62.     29-10-95    digulla automatically created from
  63.                 dos_lib.fd and clib/dos_protos.h
  64.  
  65. *****************************************************************************/
  66. {
  67.     AROS_LIBFUNC_INIT
  68.     AROS_LIBBASE_EXT_DECL(struct DosLibrary *,DOSBase)
  69.  
  70.     /* Get pointer to result. */
  71.     LONG *result=&((struct Process *)FindTask(NULL))->pr_Result2;
  72.  
  73.     /* Get pointer to filehandle */
  74.     struct FileHandle *fh=(struct FileHandle *)BADDR(file);
  75.  
  76.     /* Check if file is in write mode */
  77.     if(!(fh->fh_Flags&FHF_WRITE))
  78.     {
  79.     if(fh->fh_Pos<fh->fh_End)
  80.         /* Read mode. Try to seek back to the current position. */
  81.         if(Seek(file,fh->fh_Pos-fh->fh_End,OFFSET_CURRENT)<0)
  82.         {
  83.         fh->fh_Pos=fh->fh_End=fh->fh_Buf;
  84.         return EOF;
  85.         }
  86.  
  87.     /* Is there a buffer? */
  88.     if(fh->fh_Buf==NULL)
  89.     {
  90.         /* No. Get one. */
  91.         fh->fh_Buf=AllocMem(IOBUFSIZE,MEMF_ANY);
  92.         if(fh->fh_Buf==NULL)
  93.         {
  94.         /* Couldn't get buffer. Return error. */
  95.         *result=ERROR_NO_FREE_STORE;
  96.         return EOF;
  97.         }
  98.         /* Got it. Use it. */
  99.         fh->fh_Flags|=FHF_BUF;
  100.         fh->fh_Size=IOBUFSIZE;
  101.     }
  102.  
  103.     /* Prepare buffer */
  104.     fh->fh_Pos=fh->fh_Buf;
  105.     fh->fh_End=fh->fh_Buf+fh->fh_Size;
  106.     fh->fh_Flags|=FHF_WRITE;
  107.     }
  108.  
  109.     /* Check if there is still some space in the buffer */
  110.     if(fh->fh_Pos>=fh->fh_End)
  111.     {
  112.     UBYTE *pos;
  113.  
  114.     /* Write the data. (In many pieces if the first one isn't enough). */
  115.     pos=fh->fh_Buf;
  116.     while(pos!=fh->fh_Pos)
  117.     {
  118.         LONG size;
  119.         size=Write(file,pos,fh->fh_Pos-pos);
  120.  
  121.         /* An error happened? No success. */
  122.         if(size<0)
  123.         {
  124.         fh->fh_Pos=fh->fh_End=fh->fh_Buf;
  125.         fh->fh_Flags&=~FHF_WRITE;
  126.         return EOF;
  127.         }
  128.         pos+=size;
  129.     }
  130.  
  131.     /* Reset buffer */
  132.     fh->fh_Pos=fh->fh_Buf;
  133.     }
  134.  
  135.     /* Write data and return */
  136.     *fh->fh_Pos++=character;
  137.     return character;
  138.     AROS_LIBFUNC_EXIT
  139. } /* FPutC */
  140.